/**
 * 商户管理页面js
 * 版权所有：上海戈品投资管理有限公司
 *   author king
     功能说明：商户管理页面
     创建日期：2015-09-28
     修改记录：
     备注说明：
 */



$(document).ready(function() {
	merchantList();
});

/**
 * 查询商户列表
 * @returns
 */
var merchantList = function(){
	var datas = {"p":"merchantManage","m":"findMerchantList" };//请求参数
	var columnDefine = {"id":"ID","name":"商户名称","merchantNo":"商户编号","status":"状态"};//表格栏目定义
	var formatters = {//自定义表格格式
            "status": function (column, row)
            {
            	var txt ='';
            	switch (row.status) {
				case 1:
					txt='未审核';
					break;
				case 2:
					txt='审核';
					break;
				case 3:
					txt='禁用';
					break;
				case 4:
					txt='删除';
					break;
				default:
					break;
				}
                return  txt;
            },
			"type": function (column, row)
			{
				var txt ='';
				switch (row.type) {
				case 1:
					txt='托管';
					break;
				case 2:
					txt='对接';
					break;
				default:
					break;
				}
				return  txt;
			},
            "dateTime": function (column, row)
            {
            	var date = new Date( row.createTime );
            	return date.getFullYear()+'-' + getTheSureTime((date.getMonth()+1))+ '-' + getTheSureTime(date.getDate()) +
  	           ' ' + getTheSureTime(date.getHours())+':'+getTheSureTime(date.getMinutes())+':'+getTheSureTime(date.getSeconds());
            }
	};
	
	function getTheSureTime(emp){
		return emp>9?emp:"0"+emp;
	};
	
	
	var buttons =''; //操作按钮
	buttons+='<p>';
	buttons+='<button type=\'button\' class=\'btn btn-w-m btn-primary\' onclick="showProject(this);">查看项目</button>&nbsp;&nbsp;';
	buttons+='<button type=\'button\' class=\'btn btn-w-m btn-info\' onclick="showModifyWin(this);">编辑</button>';
	buttons+='</p>';
	
	hui.pager('service',datas ,"merchantList", columnDefine , formatters , buttons ); //分页查询
	
	$('#merchantList .warning').addClass('warning1');
};

var showProject = function(obj){
	var data = $(obj).parent().parent().find('.data').text();
	var json = eval('('+data+')');
	window.location.href='page?p=page/merchantProjectList&mid='+json.id;
};

/**
 * 打开新建窗口
 * @returns
 */
var showNewWin = function(){
	$('#newMerchantPop input').val('');
	$('#newMerchantPop').modal('show');
};

/**
 * 打开编辑窗口
 * @returns
 */
var showModifyWin = function(obj){
	
	//var checks=$('input[name="select"]:checked');
	
	var data = $('#pop_win .data').text();
	
	if( data )
	{
		//var id = checks[0].value;
/*		if( id == 'all' && checks.length > 1 )
		{
			id = checks[1].value
		}*/
		
		//var json = common.ajaxRequest('service?p=merchantManage&m=findMerchantById&id='+id, {}, 'GET');
		var json = eval('('+data+')');
		//if( json && json.result == 0 )
		if( json )
		{
			var m = json;
			$('#modifyMerchantPop .id').val(m.id);
			$('#modifyMerchantPop .name').val(m.name);
			$('#modifyMerchantPop .merchantNo').val(m.merchantNo);
			$('#modifyMerchantPop .type').val(m.type);
			$('#modifyMerchantPop .username').val(m.username);
			$('#modifyMerchantPop .password').val(m.password);
			$('#modifyMerchantPop .contact').val(m.contact);
			$('#modifyMerchantPop .tel').val(m.tel);
			$('#modifyMerchantPop .status').val(m.status);
			$('#modifyMerchantPop .address').val(m.address);
			$('#modifyMerchantPop .email').val(m.email);
			$('#modifyMerchantPop .remark').val(m.remark);
			$('#pop_win').modal('hide');
			$('#modifyMerchantPop').modal('show');
			//$('#modifyMerchantPop').parent().addClass('modal-open');
			
		}
		else
		{
			common.showMsg('error', '' , '查询出错!');
		}	
		
	}	
	else
	{
		common.showMsg('error', '' , '请选择要编辑的商户!');
	}	
	
};

/**
 * 新建商户
 * @returns
 */
var newMerchant = function(){
	
	/* 校验表单*/
	if(!validator.validate('#newMerchantPop'))
	{
		return;
	}	
	
	var name = $('#newMerchantPop .name').val();
	var merchantNo = $('#newMerchantPop .merchantNo').val();
	var type = $('#newMerchantPop .type').val();
	var username = $('#newMerchantPop .username').val();
	var password = $('#newMerchantPop .password').val();
	var contact = $('#newMerchantPop .contact').val();
	var tel = $('#newMerchantPop .tel').val();
	var address = $('#newMerchantPop .address').val();
	var email = $('#newMerchantPop .email').val();
	var remark = $('#newMerchantPop .remark').val();
	//var datas = {"name":name,"merchantNo":merchantNo,"type":type,"username":username,"password":password,"contact":contact,"tel":tel,"address":address,"email":email,"remark":remark};
	var params = 'p=merchantManage&m=addMerchant&name='+name+'&merchantNo='+merchantNo+'&type=' + type +
	                      '&username='+username+'&password='+password+'&contact='+contact+'&tel='+tel+'&address='+address+
	                      '&email='+email+'&remark='+remark;
	
	var json = common.ajaxRequest('service?'+params, null, 'GET');
	if( json && json.result == 0 )
	{
		common.showMsg('info', '创建成功', '创建成功');
		merchantList();
		$('#newMerchantPop').modal('hide');
	}
	else
	{
		common.showMsg('error', '创建失败', json.message);
	}	
};

/**
 * 编辑商户
 * @returns
 */
var modifyMerchant = function(){
	
	/* 校验表单*/
	if(!validator.validate('#modifyMerchantPop'))
	{
		return;
	}	
	
	var id = $('#modifyMerchantPop .id').val();
	var name = $('#modifyMerchantPop .name').val();
	var merchantNo = $('#modifyMerchantPop .merchantNo').val();
	var type = $('#modifyMerchantPop .type').val();
	var status = $('#modifyMerchantPop .status').val();
	//var username = $('#modifyMerchantPop .username').val();
	//var password = $('#modifyMerchantPop .password').val();
	var contact = $('#modifyMerchantPop .contact').val();
	var tel = $('#modifyMerchantPop .tel').val();
	var address = $('#modifyMerchantPop .address').val();
	var email = $('#modifyMerchantPop .email').val();
	var remark = $('#modifyMerchantPop .remark').val();
	//var datas = {"name":name,"merchantNo":merchantNo,"type":type,"username":username,"password":password,"contact":contact,"tel":tel,"address":address,"email":email,"remark":remark};
	var params = 'p=merchantManage&m=modifyMerchant&name='+name+'&merchantNo='+merchantNo+'&type=' + type +
	'&status='+status +'&contact='+contact+'&tel='+tel+'&address='+address+
	'&email='+email+'&remark='+remark +'&id='+id;
	
	var json = common.ajaxRequest('service?'+params, null, 'GET');
	if( json && json.result == 0 )
	{
		common.showMsg('info', '修改成功', '修改成功');
		//window.location.href=window.location.href;
		merchantList();
		$('#modifyMerchantPop').modal('hide');
	}
	else
	{
		common.showMsg('error', '修改失败', json.message);
	}	
};
